package com.ld.security.common.core.util;

import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TemplateUtil {

	private static final VelocityEngine ve;
    private static final int charMargin = 32;
	static {
		ve = new VelocityEngine();
		ve.init();
	}
	static {
		//设置资源加载器
		Properties prop = new Properties();
		prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		prop.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
		prop.setProperty(Velocity.INPUT_ENCODING, "UTF-8");

		Velocity.init(prop);
	}


    /**
     *
     * @param text 字符串模板
     * @param params  参数
     * @return
     */
    public static String renderString(String text,Map<String,Object> params) {
        VelocityContext context = new VelocityContext();
        for(Map.Entry<String, Object> e: params.entrySet()) {
            context.put(e.getKey(), e.getValue());
        }
        StringWriter writer = new StringWriter();
        ve.evaluate(context, writer, "", text); // 关键方法
        return writer.toString();
    }

        /**
         *
         * @param templatePath 模板路径
         * @param args 参数
         * @return
         */
    public static String renderTemplate(String templatePath,Map<String,Object> args)  {
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(templatePath, "UTF-8");
        VelocityContext context = new VelocityContext(args);
        tpl.merge(context, sw);
        return sw.toString();
    }

    public static boolean isNull(String s){
        if(s == null || s.length() == 0) {
            return true;
        }
        return false;
    }

    //首字母转小写
    public static char toLowerCaseFirstOne(char c){
        if(c >= 'A' && c <= 'Z'){
            c += charMargin;
        }
        return c;
    }
    //首字母转大写
    public static char toUpperCaseFirstOne(char c){
        if(c >= 'a' && c <= 'z'){
            c -= charMargin;
        }
        return c;
    }
    //首字母转小写
    public static String toLowerCaseFirstOne(String s){
        char[] cs = s.toCharArray();
        cs[0] = toLowerCaseFirstOne(cs[0]);

        return new String(cs);
    }


    //首字母转大写
    public static String toUpperCaseFirstOne(String s){
        char[] cs = s.toCharArray();
        cs[0] = toUpperCaseFirstOne(cs[0]);

        return new String(cs);
    }



    @SneakyThrows
    public static byte[] createTemplateZip(Map<String,String> map ){

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream out = new ZipOutputStream(outputStream);

            map.forEach((name,val) -> {
                try{
                    out.putNextEntry(new ZipEntry(name));
                    IOUtils.write(val, out, "UTF-8");
                }catch (Exception e){
                    e.printStackTrace();
                }
            });


        IOUtils.closeQuietly(out);
        return outputStream.toByteArray();
    }


        public static String javaToColumn(String columnName){
        char[] cs = columnName.toCharArray();
        StringBuffer sb = new StringBuffer();
        for(char c:cs){
            if(c >='A' && c<='Z'){
                sb.append("_"+TemplateUtil.toLowerCaseFirstOne(c));
            }else{
                sb.append(c);
            }

        }
        return sb.toString();
    }

    public static String columnToJava(String columnName){
        char[] cs = columnName.toCharArray();
        StringBuffer sb = new StringBuffer();
        boolean b = false;
        for(char c:cs){
            if(c == '_'){
                b = true;
                continue;
            }
            if(b){
                c = toUpperCaseFirstOne(c);
                b=false;
            }
            sb.append(c);

        }
        return sb.toString();
    }


    public static String tableToClass(String tableName){
        tableName = columnToJava(tableName);
        return toUpperCaseFirstOne(tableName);
    }




}
